﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;


namespace Continental.Code.Utility
{
    public class HelpMethod
    {
        public static string MD5(string input)
        {
            System.Security.Cryptography.MD5CryptoServiceProvider md5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] hashedBytes;
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(input));

            StringBuilder s = new StringBuilder();
            foreach (byte p in hashedBytes)
            {
                s.Append(p.ToString("x").ToLower());
            }
            return s.ToString();
        }

        public static bool IsEmail(string input)
        {
            Regex RegexObj = new Regex(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$");
            return RegexObj.Match(input).Success;
        }

        public static bool IsUserID(string input)
        {
            Regex RegexObj = new Regex(@"[a-zA-Z_]+[a-zA-Z0-9_]");
            return RegexObj.Match(input).Success;
        }

        public static bool IsPassword(string input)
        {
            Regex RegexObj = new Regex(@"[a-zA-Z0-9_@$]");
            return RegexObj.Match(input).Success;
        }

        public static string ChangeDatetimeToString(DateTime dt)
        {
            string result;
            long tickDiff = DateTime.Now.Ticks - dt.Ticks;
            tickDiff = tickDiff / 10000000; //have seconds now
            int day = (int)(tickDiff / 86400);
            if (day == 0)
                result = dt.Hour + "h" + dt.Minute + "'|Hôm nay";
            else if (day == 1)
                result = dt.Hour + "h" + dt.Minute + "'|Hôm qua";
            else if (day == 2)
                result = dt.Hour + "h" + dt.Minute + "'|Hôm kia";
            else
                result = "cách đây " + dt.ToString() + " ngày";
            return result;
        }

        public static string CatString(string input, int max)
        {
            if (input.Length > max)
                return input.Substring(0, max) + "...";
            else
                return input;
        }

        public static string RemoveHtmlTag(string input)
        {
            input = input.Replace("&lt;", "<");
            input = input.Replace("&gt;", ">");
            input = input.Replace("&nbsp;", " ");

            char[] array = new char[input.Length];
            int arrayIndex = 0;
            bool inside = false;

            for (int i = 0; i < input.Length; i++)
            {
                char let = input[i];
                if (let == '<')
                {
                    inside = true;
                    continue;
                }
                if (let == '>')
                {
                    inside = false;
                    continue;
                }
                if (!inside)
                {
                    array[arrayIndex] = let;
                    arrayIndex++;
                }
            }
            return new string(array, 0, arrayIndex);
        }



        public static string RemoveSign4VietnameseString(string str)
        {
            string[] VietnameseSigns = new string[]
            {
                "aAeEoOuUiIdDyY",
                "áàạảãâấầậẩẫăắằặẳẵ",
                "ÁÀẠẢÃÂẤẦẬẨẪĂẮẰẶẲẴ",
                "éèẹẻẽêếềệểễ",
                "ÉÈẸẺẼÊẾỀỆỂỄ",
                "óòọỏõôốồộổỗơớờợởỡ",
                "ÓÒỌỎÕÔỐỒỘỔỖƠỚỜỢỞỠ",
                "úùụủũưứừựửữ",
                "ÚÙỤỦŨƯỨỪỰỬỮ",
                "íìịỉĩ",
                "ÍÌỊỈĨ",
                "đ",
                "Đ",
                "ýỳỵỷỹ",
                "ÝỲỴỶỸ"
            };

            //Tiến hành thay thế , lọc bỏ dấu cho chuỗi

            for (int i = 1; i < VietnameseSigns.Length; i++)
            {

                for (int j = 0; j < VietnameseSigns[i].Length; j++)

                    str = str.Replace(VietnameseSigns[i][j], VietnameseSigns[0][i - 1]);

            }

            return str;
        }

        public static string ConvertToUnSignForUrl(string text)
        {
            for (int i = 32; i < 48; i++)
            {
                text = text.Replace(((char)i).ToString(), " ");
            }

            text = text.Replace(".", "-");
            text = text.Replace(" ", "-");
            text = text.Replace(",", "-");
            text = text.Replace(";", "-");
            text = text.Replace(":", "-");
            text = text.Replace("&", "-");
            text = text.Replace("?", "-");
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = text.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }
    }
}
